﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DAL;
using CommonUtility;
using Model;

namespace BLL
{
    public class MailHelper
    {
        public static IList<MailInfo> GetMailList_noSend()
        {
            return Mail.GetList("isNull([sTime],'') = ''");
        }

        /// <summary>
        /// 新增邮件
        /// </summary>
        /// <param name="_address">邮件地址</param>
        /// <param name="_title">邮件主题</param>
        /// <param name="_body">邮件内容</param>
        /// <param name="_myTran"></param>
        public static void AddNew(string _address, string _title, string _body, SqlTransaction _myTran)
        {
            MailInfo mail = new MailInfo();
            mail.Address = _address;
            mail.Subject = _title;
            //mail.Body = _body + "<br/><br/>This email was generated from the RFQS website. Please do not reply<br/>本邮件由系统自动生成，请不要回复邮件。";
            mail.Body = _body + "<br/><br/>This email was generated from the RFQS website. Please do not reply.";
            mail.CDate = DateTime.Now.ToString();
            Mail.Add(mail, _myTran);
        }




        public static string MailTo(string _toMail, string _title, string _content, SystemConfig _config)
        {
            string result = string.Empty;
            if (!string.IsNullOrEmpty(_toMail))
            {
                try
                {
                    _toMail = _toMail.Replace("，", ",").Trim().Trim(',').Trim();
                    System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
                    //2014-11-17 jerzy 修改
                    //client.Host = "mail.teczs.com";
                    //client.UseDefaultCredentials = true;
                    client.Host = _config.SMTP;
                    client.UseDefaultCredentials = false;

                    client.Credentials = new System.Net.NetworkCredential(_config.Email, _config.Password);
                    client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

                    string[] arrMail = _toMail.Split(',');
                    foreach (string mail in arrMail)
                    {
                        if (!string.IsNullOrEmpty(mail))
                        {
                            try
                            {
                                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(_config.Email, mail);
                                message.Subject = _title;
                                message.Body = _content;
                                message.BodyEncoding = System.Text.Encoding.UTF8;
                                message.IsBodyHtml = true;
                                message.Priority = System.Net.Mail.MailPriority.High;//邮件优先级
                                client.Send(message);
                                Common.TraceApp(mail + "\t\t" + _title);
                            }
                            catch (Exception exx)
                            {
                                result = "Err";
                                Common.Application_Error_Server("MailHelper/MailTo()\n" + mail + "\n" + _title + "\n" + _content, exx);
                                
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Common.Application_Error_Server("MailHelper/MailTo()\n" + _toMail + "\n" + _title + "\n" + _content, ex);
                    result = "Err";
                }
            }
            return result;
        }


        /// <summary>
        /// 发送邮件
        /// </summary>
        public static void ScanMail()
        {
            try
            {
                SystemConfig config = new SystemConfig();
                IList<MailInfo> mailList = Mail.GetList("inull(sTime,'') = ''");
                foreach (MailInfo mail in mailList)
                {
                    try
                    {
                        string result = MailTo(mail.Address, mail.Subject, mail.Body, config);
                        if (string.IsNullOrEmpty(result))
                        {
                            mail.STime = DateTime.Now.ToString();
                            Mail.Edit(mail);
                        }
                    }
                    catch (Exception ex1)
                    {
                        Common.Application_Error_Server("MailHelper/ScanMail(1)\n" + mail.Address + "\n" + mail.Subject + "\n" + mail.Body, ex1);
                    }
                }
            }
            catch (Exception ex2)
            {
                Common.Application_Error_Server("MailHelper/ScanMail(2)", ex2);
            }
        }

    }
}
